package cn.exrick.manager.task;

import cn.exrick.common.utils.ExportExcel;
import cn.exrick.manager.dto.OrderRanking;
import cn.exrick.manager.pojo.TbReport;
import cn.exrick.manager.service.OrderService;
import cn.exrick.manager.service.ReportService;
import cn.hutool.core.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 部门申领金额月报定时任务
 */
@Component
public class OrgMonthReportJob {

    private static final Logger log= LoggerFactory.getLogger(OrgMonthReportJob.class);

    @Autowired
    private OrderService orderService;
    @Autowired
    private ReportService reportService;
    @Value("${FILE_STORE_PATH}")
    private String storePath;


    @Scheduled(cron = "0 0 2 1 * ?")
    public void run() {

        log.info("开始执行部门申领金额月报定时任务 - " + DateUtil.now());
        Date date = new Date();
        String startTime = DateUtil.formatDateTime(DateUtil.beginOfMonth(date));
        String endTime = DateUtil.formatDateTime(DateUtil.endOfMonth(date));
        List<OrderRanking> orderRankingList = orderService.orgApplyStatistic(startTime, endTime);
        String[] rowName = new String[]{"部门名称","申领商品数量(件)","申领总金额(元)"};
        List<Object[]> dataList = new ArrayList<>();
        for(OrderRanking orderRanking : orderRankingList){
            Object[] data = new Object[]{orderRanking.getOrganizationName(),orderRanking.getApplyNum(),orderRanking.getApplyAmount()};
            dataList.add(data);
        }
        int year = DateUtil.thisYear();
        int month = DateUtil.thisMonth() + 1;
        String fileName = "部门申领金额月报（"+year+"年"+month+"月）";
        ExportExcel exportExcel = new ExportExcel(fileName,rowName,dataList);
        File file = new File(storePath + File.separator + fileName + ".xls");
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        OutputStream out = null;
        try {
            out = new FileOutputStream(file);
            exportExcel.export(out);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        TbReport tbReport = new TbReport();
        tbReport.setYear(year);
        tbReport.setMonth(month);
        tbReport.setTitle(fileName);
        tbReport.setCreateTime(new Date());
        tbReport.setUpdateTime(new Date());
        reportService.addReport(tbReport);
        log.info("部门申领金额月报定时任务执行完成 - " + DateUtil.now());
    }

    public static void main(String[] args) {
        Date date = new Date();

        System.out.println(DateUtil.formatDateTime(DateUtil.beginOfMonth(date)));
        System.out.println(DateUtil.formatDateTime(DateUtil.endOfMonth(date)));
    }

}
